//
//  ZLZKAppDelegate.m
//  ZLZKBaseUtils
//
//  Created by 1134548344@qq.com on 07/06/2020.
//  Copyright (c) 2020 1134548344@qq.com. All rights reserved.
//

#import "ZLZKAppDelegate.h"
#import "ZLZKViewController.h"
#import "ZLZKTestViewController.h"
#import "ZLZKBaseUtils.h"
#import "SXHRedirectRequestUrlProtocol.h"
#import "URLProtocolHandler.h"
#import "ZLZKWKwebviewControllerViewController.h"

@implementation ZLZKAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    
    [NSURLProtocol registerClass:[SXHRedirectRequestUrlProtocol class]];
    //设置配置文件目录
    [[URLProtocolHandler shared] setConfigure:[[NSBundle mainBundle] pathForResource:@"protocol" ofType:@""]];
    
    Class cls = NSClassFromString(@"WKBrowsingContextController");
    SEL sel = NSSelectorFromString(@"registerSchemeForCustomProtocol:");
    if ([cls respondsToSelector:sel]) {
#pragma clang diagnostic push

#pragma clang diagnostic ignored"-Warc-performSelector-leaks"
        [cls performSelector:sel withObject:@"ZLZKCRM-DEV"];
#pragma clang diagnostic pop
    }
    
    _window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
    
    _window.backgroundColor = [UIColor whiteColor];

    ZLZKViewController *vc = [ZLZKViewController new];
//    ZLZKTestViewController *next = [ZLZKTestViewController new];
    
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    
//    ZLZKWKwebviewControllerViewController *vc1 = [ZLZKWKwebviewControllerViewController new];
    
    _window.rootViewController = nav;
    
    [_window makeKeyAndVisible];
    
    [[NSUserDefaults standardUserDefaults] setValue:@"0A1909465B1B987105A8AE3F6F964842-n1" forKey:@"sessionId"];
    
    // 初始化并注册通知
    return [[ZLZKBaseUtils sharedInstace] initWithLaunchOptions:launchOptions];
    
//    return YES;
}


#pragma mark - 通知相关 获取deviceToken

// 注册远程通知成功后，会调用这个方法，把 deviceToken 返回给我们
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    
    [[ZLZKBaseUtils sharedInstace] registerForRemoteNotificationsWithDeviceToken:deviceToken];
}

// 注册远程通知失败后，会调用这个方法
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    
    [[ZLZKBaseUtils sharedInstace] failToRegisterForRemoteNotificationsWithError:error];
}

// 当接收到远程通知（实现了这个方法，则上面的方法不再执行）
// 前台（会调用）
// 从后台进入到前台（会调用）
// 完全退出再进入APP （也会调用这个方法）
//
// 如果要实现：只要接收到通知，不管当前应用在前台、后台、还是锁屏，都执行这个方法
//    > 必须勾选后台模式 Remote Notification
//    > 告诉系统是否有新的内容更新（执行完成代码块）
//    > 设置发送通知的格式 {"content-available" : "随便传"} （在 aps 键里面设置）
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    NSLog(@"收到远程通知2----%@", userInfo);

    // 调用系统回调代码块的作用
    //  > 系统会估量app消耗的电量，并根据传递的 `UIBackgroundFetchResult` 参数记录新数据是否可用
    //  > 调用完成的处理代码时，应用的界面缩略图会更新
    completionHandler(UIBackgroundFetchResultNewData);
}

@end
